<?php

/**
 * Настройка View в соответсятвии с файлом конфигурации
 */
class Extends_Controller_Plugin_SetUpLayout
        extends Zend_Controller_Plugin_Abstract {

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {

        $layout = Zend_Layout::getMvcInstance();
        $front = Zend_Controller_Front::getInstance();
        $bootstrap = $front->getParam('bootstrap');
        $options = $bootstrap->getOptions();
        $view = $bootstrap->getResource('view');
        $module = $request->getModuleName();


        //Get Global layout and view settings from config
        $default_layout = $options['resources']['layout'];
        $default_view = $options['resources']['view'];

        //Get personal module settings layout and view from config
        if (isset($options['resources']['modules'][$module]['resources'])) {
            if (isset($options['resources']['modules'][$module]['resources']['layout'])) {
                $options_layout = $options['resources']['modules'][$module]['resources']['layout'];
            }
            else {
                $options_layout = null;
            }
            if (isset($options['resources']['modules'][$module]['resources']['view'])) {
                $options_view = $options['resources']['modules'][$module]['resources']['view'];
            }
            else {
                $options_view = null;
            }
        }

        //set layout
        if (isset($options_layout)) {
            if ($options_layout['layout'] != null) {
                $layout->setLayout($options_layout['layout']);
            }
        }
        else {
            $layout->setLayout($default_layout['layout']);
        }

        //get layout path
        $layoutPath = isset($default_layout['layoutPath']) ? (array) $default_layout['layoutPath'] : array();

        if (isset($options_layout)) {
            if (isset($options_layout['layoutPath'])) {
                foreach ((array) $options_layout['layoutPath'] as $key => $value) {
                    $layoutPath[] = APPLICATION_PATH . '/modules/' . $module . '/layout/';
                }
            }
        }

        //set layout path
        $layout->setLayoutPath($layoutPath);

        //set doctype
        if (isset($options_view['doctype'])) {
            $view->doctype($options_view['doctype']);
        }
        else {
            $view->doctype($default_view['doctype']);
        }

        //set content type
        if (isset($options_view['contentType'])) {
            $view->headMeta()->appendHttpEquiv('Content-Type', $options_view['contentType']);
        }
        else {
            $view->headMeta()->appendHttpEquiv('Content-Type', $default_view['contentType']);
        }

        //set title
        if (isset($options_view['title']) & !empty($options_view['title'])) {
            $view->headTitle($options_view['title']);
        }
        else {
            $view->headTitle($default_view['title']);
        }

        //set title separator
        if (isset($options_view['titleSeparator'])) {
            $view->headTitle()->setSeparator($options_view['titleSeparator']);
        }
        else {
            $view->headTitle()->setSeparator($default_view['titleSeparator']);
        }

        //get stylesheet
        $default_css = isset($default_view['css']) ? (array) $default_view['css'] : array();
        $options_css = isset($options_view['css']) ? (array) $options_view['css'] : array();
        /* foreach ($default_css as $key => $value) {
          if (!isset($options_css[$key])) {
          $options_css[$key] = $value;
          }
          }

          //set stylesheet
          foreach ($options_css as $key => $value) {
          $view->headLink()->appendStylesheet("/css/" . $value);
          } */
        foreach ($default_css as $key => $value) {
            $view->headLink()->appendStylesheet("/css/" . $value);
        }
        foreach ($options_css as $key => $value) {
            $view->headLink()->appendStylesheet("/css/" . $module . "/" . $value);
        }

        //set cache control
        if (isset($options_view['pragmaNoCache'])) {
            $pragmaNoCache = $options_view['pragmaNoCache'];
        }
        else {
            $pragmaNoCache = $default_view['pragmaNoCache'];
        }

        if ($pragmaNoCache) {
            $view->headMeta()->appendHttpEquiv('expires', 'Wed, 26 Feb 1997 08:21:57 GMT')
                    ->appendHttpEquiv('pragma', 'no-cache')
                    ->appendHttpEquiv('Cache-Control', 'no-cache');
        }

        //set encoding
        if (isset($options_view['encoding'])) {
            $view->setEncoding($options_view['encoding']);
        }
        else {
            $view->setEncoding('utf-8');
        }

        //set helpers
        $helpers = (isset($options['resources']['view']['helpers'])) ? $options['resources']['view']['helpers'] : array();
        $view->addHelperPath($helpers['path'], $helpers['namespace']);
    }

}
